package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    /**

     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单明细分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public  R<Page> page(int page, int pageSize, String number, String beginTime, String endTime){
        //1.接收信息
        log.info("page:{},pageSize:{}",page,pageSize);
        //2.构造分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //3.构造条件构造器
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.like(number!=null,Orders::getId,number);
        ordersLambdaQueryWrapper.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        ordersLambdaQueryWrapper.le(endTime!=null,Orders::getOrderTime,endTime);
        //4.添加排序条件
        ordersLambdaQueryWrapper.orderByDesc(Orders::getCheckoutTime);
        //5.执行查询
        ordersService.page(ordersPage,ordersLambdaQueryWrapper);
        List<Orders> records = ordersPage.getRecords();
        records = records.stream().map(item->{
            item.setUserName("用户"+item.getUserId());
            return item;
        }).collect(Collectors.toList());
        return R.success(ordersPage);
    }

    /**
     * 修改状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> updateOrder(@RequestBody Orders orders){
        //1.接收数据
        log.info("订单orders派送",orders);
        //2.修改状态
        boolean b = ordersService.updateById(orders);
        return b?R.success("订单派送成功"):R.error("订单派送失败");
    }
    @GetMapping("/userPage")
    public R userPage(Integer page, Integer pageSize){
        //1.接收信息
        log.info("page:{},pageSize:{}",page,pageSize);
        Long userId = BaseContext.getId();

        //2.构造分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);
        //3.构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,Orders::getUserId,userId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //4.执行查询
        ordersService.page(pageInfo,queryWrapper);
        //5.对象拷贝
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map(item->{
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            Long id = item.getId();
            //根据订单号查询订单详细
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderDetail::getOrderId,id);
            //执行查询
            List<OrderDetail> orderDetails = orderDetailService.list(wrapper);
            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(list);

        return R.success(ordersDtoPage);
    }
}
